<template>
	<view>
		<u-upload :file-list="fileList" @on-remove='onRemove' ref='upPositive' @on-success='onSuccess' :header='header'
		 max-count="1" :action="action"></u-upload>
	</view>
</template>

<script>
	import {
		baseURL,
		baseImgUrl,
		Appid
	} from '@/config/config.js'
	export default {
		props: {
			defaultUrl: ''
		},
		watch: {
			defaultUrl(newVal, oldval) {
				if (newVal && !this.imgUrl) {
					this.fileList = [{
						url: newVal
					}]
				}

			}
		},
		data() {
			return {
				fileList: [],
				header: {
					MyToken: ''
				},
				imgUrl: '',
				action: baseURL + '/wechatMini/auth/commen/' + Appid + '/upload',
			};
		},

		mounted() {
			this.header.MyToken = this.cache.getCache('token')
		},
		methods: {
			onRemove() {
				this.$emit('onSuccess', {
					data: ''
				})
			},
			onSuccess(res) {
				if (res.code == '405') {
					this.cache.removeCache('token')
					this.cache.removeCache('nickName')
					this.$refs.upPositive.clear()
					uni.showToast({
						title: '请登录后继续操作',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login',
							fail: (err) => {
								console.log(err)
							}
						})
					}, 2000)
				} else if (res.code == 200) {
					this.$emit('onSuccess', res)
					this.imgUrl = res.data
				} else {
					uni.showToast({
						title: '发生未知错误，请稍微再试',
						icon: 'none'
					})
					this.$refs.upPositive.clear()
				}
			},
		}
	}
</script>

<style lang="scss">

</style>
